//
//  MainViewController.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/9/20.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 懒加载属性
    private lazy var imageNames = ["tabbar_icon_home","tabbar_icon_all","","tabbar_icon_friend","tabbar_icon_me"]
    
//    private lazy var composeBtn : UIButton = UIButton.createButton(bgImageName: "tabbar_icon_publish")
    //构造方法创建
    private lazy var composeBtn : UIButton = UIButton(bgImageName: "tabbar_icon_publish")
    
    // MARK: - 系统调用函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupComposeBtn()
        
        configTabbarIOS15()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //必须放在viewWillAppear实现,否则系统会初始化tabbar
        setupTabbarItems()
        
    }
    
    
    
}

//设置UI界面
extension MainViewController {
    //设置发布按钮
    private func setupComposeBtn() {
        //1.将按钮添加到tabbar中
        tabBar.addSubview(composeBtn)
        
//        //2.设置属性
//        composeBtn.setBackgroundImage(UIImage(named: "tabbar_icon_publish"), for: .normal)
//        composeBtn.sizeToFit()
        
        //3.设置位置
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        
        //4.监听按钮的点击
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
        
    }
    
    //调整tabbar中的item
    private func setupTabbarItems() {
        //1.遍历所有的item
        for i in 0..<tabBar.items!.count {
            //2.获取item
            let item = tabBar.items![i]

            //3.如果下标值为2,则该item不可以和用户交互
            if i == 2 {
                item.isEnabled = false
                continue
            }
            
            //4.设置其他item的选中时候的图片
            item.selectedImage = UIImage(named: imageNames[i] + "_selected")
        }
    }
}

//时间监听
extension MainViewController {
    //事件监听本质发送消息,但是发送消息是OC的特性
    //将方法包装成 SEL --》 类中查找方法列表 --〉 根据 SEL查找imp指针(函数指针) --》 执行函数
    //如果swift中将一个函数声明为private,那么该函数不会被添加到方法列表中
    //如果在private前面加上 objc,那么该方法依然会被添加到方法列表中
    @objc private func composeBtnClick() {
        print("composeBtnClick")
        
        
//        NetworkTools.shareInstance.request(methodType: .GET, urlString: "http://httpbin.org/get", parameters: ["name" : "wubin", "age" : "29"]) { result, error in
//            print(result!)
//        }
        
        
        
    }
}

//处理iOS15Tabbar颜色变成透明
extension MainViewController {
    func configTabbarIOS15() {
       if #available(iOS 15.0, *) {
           let appearance = UITabBarAppearance()
           appearance.configureWithOpaqueBackground()
           appearance.backgroundColor = UIColor.white
           tabBar.standardAppearance = appearance
           tabBar.scrollEdgeAppearance = appearance
       }
   }
}


